using System;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif

namespace UnityXEditor
{
    [Serializable]
    public class iOSToolSettings
    {
#if UNITY_IOS
        //构建iOS之后处理
        [PostProcessBuild]
        public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
        {
            //修改Info.plist文件
            PlistDocument plist = new PlistDocument();
            plist.ReadFromFile(pathToBuiltProject + "/Info.plist");
            {
                //APP语言
                switch (UnityXSettings.platformTool.iOS.m_Language)
                {
                    case Language.en:
                        plist.root.SetString("CFBundleDevelopmentRegion", "en");
                        break;
                    case Language.zh_Hans:
                        plist.root.SetString("CFBundleDevelopmentRegion", "zh-Hans");
                        break;
                    default:
                        break;
                }
                //出口合规证明不使用加密
                if (UnityXSettings.platformTool.iOS.m_ITSAppUsesNonExemptEncryption)
                {
                    plist.root.SetBoolean("ITSAppUsesNonExemptEncryption", false);
                }
                //添加权限
                if (UnityXSettings.platformTool.iOS.m_PhotoLibrary)
                {
                    plist.root.SetString("NSPhotoLibraryUsageDescription", UnityXSettings.platformTool.iOS.m_PhotoLibraryDescription);
                }
            }
            plist.WriteToFile(pathToBuiltProject + "/Info.plist");
        }
#endif
        public enum Language
        {
            en,
            zh_Hans,
        }
        [SerializeField]
        public Language m_Language = Language.zh_Hans;
        [SerializeField]
        public bool m_ITSAppUsesNonExemptEncryption = false;

        [SerializeField]
        public bool m_PhotoLibrary = false;
        public string m_PhotoLibraryDescription = "保存拍照和录像";

        public void OnGUI()
        {
            m_Language = (Language)EditorGUILayout.EnumPopup("设置语言和区域", m_Language);
            m_ITSAppUsesNonExemptEncryption = EditorGUILayout.Toggle("出口合规证明不使用加密", m_ITSAppUsesNonExemptEncryption);

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("添加权限");
            EditorGUI.indentLevel++;
            DrawPermission("相册权限", ref m_PhotoLibrary, ref m_PhotoLibraryDescription);
            EditorGUI.indentLevel--;
        }
        public void Update()
        {
        }
        private void DrawPermission(string name, ref bool b, ref string s)
        {
            EditorGUILayout.BeginHorizontal();
            b = EditorGUILayout.Toggle(name, b);
            EditorGUI.BeginDisabledGroup(!b);
            s = EditorGUILayout.TextField(s);
            EditorGUI.EndDisabledGroup();
            EditorGUILayout.EndHorizontal();
        }
    }
}
